﻿using System;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.CM
{
    /// <summary>
    /// ClinetLinkmanList 的摘要说明。
    /// </summary>
    public partial class ClinetLinkmanListPage : Page
    {
        private int _clientId;

        protected override void OnInit(EventArgs e)
        {
            btn_OK.Click += btn_OK_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
                if (Session["cm_permission"].ToString() == "leader")
                {
                    HyperLink1.Visible = false;
                    btn_OK.Visible = false;
                }
                else
                {
                    HyperLink1.Visible = true;
                    btn_OK.Visible = true;
                }
                HyperLink1.NavigateUrl = "Linkman.aspx?ClientID=" + _clientId;
            }

            base.OnLoad(e);
        }

        private void BindData()
        {
            var cm = new Components.CM();
            var client = new ClientInfo();
            SqlDataReader dr;
            _clientId = ((Request.Params["clientid"] == null) || (Request.Params["clientid"].Trim() == "")) ? 0 : Int32.Parse(Request.Params["clientid"]);
            if (_clientId != 0)
            {
                dr = cm.GetLinkmanFromClient(_clientId);
                client = cm.GetClientAllInfo(_clientId);
            }
            else //如果没有传入clientid那么检查是否有临时linkman
            {
                string linkmanstring = (Session["tmplinkmanid"] == null) ? "0" : Session["tmplinkmanid"].ToString();
                if (linkmanstring.EndsWith(","))
                    linkmanstring = linkmanstring.Substring(0, linkmanstring.Length - 1);
                dr = cm.GetLinkmanByID(linkmanstring);
            }
            //			rbl_LinkmanList.DataSource = dr;
            //			rbl_LinkmanList.DataTextField = "Name";
            //			rbl_LinkmanList.DataValueField = "id";
            //			rbl_LinkmanList.DataBind();
            while (dr.Read())
            {
                var li = new ListItem();
                if (Session["cm_permission"].ToString() == "administrator")
                    li.Text = "<a href='Linkman.aspx?LinkmanID=" + dr["ID"] + "'>" + dr["Name"] + "</a>";
                else
                    li.Text = dr["Name"].ToString();

                li.Value = dr["ID"].ToString();
                rbl_LinkmanList.Items.Add(li);
            }
            dr.Close();
            //如果临时主要联系人存在则让它处于选中状态
            if (Session["tmpchief"] != null)
            {
                //找到tmpchief的index值并让它处于选中状态
                foreach (ListItem lt in rbl_LinkmanList.Items)
                {
                    if (lt.Value == Session["tmpchief"].ToString())
                        lt.Selected = true;
                }
            }
            else
            {
                foreach (ListItem lt in rbl_LinkmanList.Items)
                {
                    if (lt.Value == client.ChiefLinkmanID.ToString())
                        lt.Selected = true;
                }
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            Session["tmpchief"] = rbl_LinkmanList.SelectedItem.Value;
            var cm = new Components.CM();
            SqlDataReader dr = cm.GetLinkmanByID(rbl_LinkmanList.SelectedItem.Value);

            while (dr.Read())
            {
                //使用正则表达式分析 <a ....>....</a>得到各个属性值
                string href = "";
                string name = "";
                string linkstring = @rbl_LinkmanList.SelectedItem.Text;
                const string pattern = "href\\s*=\\s*((?<begin>('|\"))(?<href>.*?)\\k<begin>|(?<href>\\S+))(.*?)>(?<name>.*?)</a>";
                Match match;
                var thereg = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                for (match = thereg.Match(linkstring); match.Success; match = match.NextMatch())
                {
                    href = match.Groups["href"].Value;
                    name = match.Groups["name"].Value;
                }


                Response.Write("<script>opener.document.getElementById('hlk_Chiefman').href=\"" + href + "\";opener.document.getElementById('hlk_Chiefman').innerText = \"" + name + "\"</script>");
                Response.Write("<script>opener.document.getElementById('lbl_position').innerText='" + dr["position"] + "';opener.document.getElementById('lbl_chieftel').innerText='" + dr["telephone"] + "';</script>");
            }
            dr.Close();
            Response.Write("<script>window.close();</script>");
        }
    }
}